package string;

public class StringDemo {
    public static void main(String[] args) {
        String s1 = "123abc";
        String s2 = "123abc";
        System.out.println("s1:" + s1);
        System.out.println("s2:" + s2);
        System.out.println("s1==s2:" + (s1 + s2));
        String s3 = "123abc";
        System.out.println("s3:" + s3);
        System.out.println("s1==s3:" + (s1 == s3));

        String s4 = new String("123abc");
        System.out.println("s4:" + s4);
        System.out.println("s1==s4:" + (s1 == s4));

        s1 = s1 + "!"; //拼接产生的新内容不会缓存在常量值中
        System.out.println("s1:" + s1);
        System.out.println("s2:" + s2);
        System.out.println("s3:" + s3);

        /*
            触发了编译器的预编译特性
            编译器在编译源代码时，如果发现一个计算表达式可以在编译期间就确定结果时
            就会进行计算，并将结果替换该表达式
            例如:
            String s5 = "123"+"abc";   +两侧是字面量，结果可以确定
            编译器会计算，并将结果替换该表达式到字节码文件中:
            String s5 = "123abc";
         */
        String s5 = "123"+"abc";
        System.out.println("s5:"+s5);
        System.out.println("s2==s5:"+(s2==s5));//true  重用常量池对象

    }
}
